var del_case_ids = new Array();
var oper_data_ids = new Array();
var owners;
var toPage = 1;
var totalPage = 1;
var toDataPage = 1;
var totalDataPage = 1;
$(function() {
	initSearch();
	initTable();
	initDialog();
});

function initSearch() {
	toPage = 1;
	toDataPage = 1;
	$('.J_Submit').on('click', function() {
		// $("#loading2").show();//显示查询时的gif图片
		var doText = $("#doInput").val();
		if (doText == "") {
			// $("#loading2").hide();
			return;
		}
	});
	$("#prev").click(function() {
		// mark
		if (toPage > 1) {
			toPage--;
		}
	});
	$("#next").click(function() {
		if (!isEnd) {
			toPage++;
		} else {
			showEndInfo();
		}
	});
	$("#dt_row").change(function() {
		toPage = 1;
	});

	$("#prevData").click(function() {
		// mark
		if (toDataPage > 1) {
			toDataPage--;
		}
	});
	$("#nextData").click(function() {
		if (!isEnd) {
			toDataPage++;
		} else {
			showEndInfo();
		}
	});
	$("#dt_row_data").change(function() {
		toDataPage = 1;
	});
}

// 初始化表格
function initTable() {
	fetchTable('', '', '', '', 0);
	// 全选/取消全部
	$("#checkAllChange").click(function() {
		if (this.checked == true) {
			del_case_ids = [];
			$(".casestable").each(function() {
				this.checked = true;
				del_case_ids.push(this.value);
			});
		} else {
			$(".casestable").each(function() {
				this.checked = false;
			});
			del_case_ids = [];
		}
	});
}

function fetchTable(name, classes, scenes, owners, level) {
	// $('#loading').show();
	isEnd = true;
	var searchvalue = $("#doInput").val();
	var pageSize = $('#dt_row').val();
	owners = owners;
	$
			.ajax({
				url : '/smarttest-frontstage/AutoInterface/search/table.action',
				dataType : 'json',
				method : 'POST',
				data : {
					name : name,
					classes : classes,
					scenes : scenes,
					owners : owners,
					searchvalue : searchvalue,
					pageSize : pageSize,
					toPage : toPage,
					level : level
				},
				success : function(data) {
					del_case_ids = [];
					$("#tbody").html("");
					var msg = data.rows;
					var i = 0;
					var html = '';
					$
							.each(
									msg,
									function(k, v) {
										var item = msg[i];
										if (i % 2 === 0) {
											html += '<tr bgcolor="#DFEBF2">';
										} else {
											html += '<tr>';
										}
										html += '<td>'
												+ "<input type=\"checkbox\" class=\"casestable\" onclick=\"javascript:singleCheck(this);\" value=\""
												+ item.id + "\"/>" + '</td>';
										html += '<td>' + item.name + '</td>';
										html += '<td>' + item.clazz + '</td>';
										html += '<td>' + item.scens + '</td>';
										html += '<td>' + item.owner + '</td>';
										html += '<td>' + item.remark + '</td>';
										html += '<td>' + item.level + '</td>';
										html += '<td>' + item.url + '</td>';
										html += '<td>'
												+ getNowFormatDate(item.created)
												+ '</td>';
										html += '<td>'
												+ getNowFormatDate(item.updated)
												+ '</td>';
										html += '<td>'
												+ "<input type=\"button\" onclick=\"executeCase(this);\" class=\"btn btn-primary  btn-sm\"  id=\""
												+ item.id + "\" value=执行用例>"
												+ '</td>';
										html += '</tr>';
										i++;
									});
					$("#tbody").append(html);
					$('#loading').hide();
				}
			});
}

function initDialog() {
	pageAddCaseControl(false,false,true,true,true,true,true,true,true,true);
	$('#addCase').click(function() {
		addCase();
	});
	$('#isdownLoad').click(function() {
		downLoadCase($(this));
	});
}

function getAddDialog() {
	$("#caseid").val(-1);
	$("#caseurl").val("");
	$('#casemodal').modal();
	$('#isdownLoadDiv').hide();
	$('#caseupload').show();
	
}

function addCase() {
	var id = $("#caseid").val();
	var url = $("#caseurl").val();
	var name = $("#name").val();
	var clazz = $("#clazz").val();
	var scenes = $("#scenes").val();
	var owner = $("#owner").val();
	var remark = $("#remark").val();
	var level = $("#caseLevel").val();
	$.ajax({
		url : '/smarttest-frontstage/AutoInterface/case/add.action',
		dataType : 'json',
		method : 'POST',
		data : {
			id : id,
			name : name,
			clazz : clazz,
			scenes : scenes,
			owner : owner,
			remark : remark,
			level : level,
			url : url
		},
		success : function(data) {
			alertDialog('增加/修改用例', '操作成功');
		}
	});
}

function modifyCases() {
	if (del_case_ids.length != 1) {
		alertDialog('修改用例', '请选择单个设备进行修改');
		return;
	}
	var dd = $(":checkbox[value=" + del_case_ids[0] + "]");
	var id = del_case_ids[0];
	var trselected = dd.parent().parent();
	var name = trselected.children("td:eq(1)").text();
	var clazz = trselected.children("td:eq(2)").text();
	var scenes = trselected.children("td:eq(3)").text();
	var owner = trselected.children("td:eq(4)").text();
	var remark = trselected.children("td:eq(5)").text();
	var caselevel = trselected.children("td:eq(6)").text();
	var caselurl = trselected.children("td:eq(7)").text();
	$("#caseid").val(id);
	$("#name").val(name);
	$("#clazz").val(clazz);
	$("#scenes").val(scenes);
	$("#owner").val(owner);
	$("#remark").val(remark);
	$("#caselevel").val(caselevel);
	$('#casemodal').modal();
	$("#casedownLoadUrl").val(caselurl);
	$('#isdownLoadDiv').show();
	$('#caseupload').hide();
	pageAddCaseControl(false,false,false,false,false,false,false,false,false,false);
}

function singleCheck(checkbox) {
	if (checkbox.checked) {
		if (-1 == $.inArray(checkbox.value, del_case_ids)) {
			del_case_ids.push(checkbox.value);
		}
	} else {
		var i = $.inArray(checkbox.value, del_case_ids);
		if (i > -1) {
			del_case_ids.splice(i, 1);
		}
	}
}

function deleteCase() {
	if (del_case_ids.length < 1) {
		alertDialog("删除用例", "请选择一个用例再删除");
		return;
	}
	var info = '';
	for (var i = 0; i < del_case_ids.length; i++) {
		// 如果i+1等于选项长度则取值后添加空字符串，否则为逗号
		info = (info + del_case_ids[i])
				+ (((i + 1) == del_case_ids.length) ? '' : ',');
	}
	$
			.SmartMessageBox(
					{
						title : "<i class='fa fa-sign-out txt-color-orangeDark'></i>Delete Case<span class='txt-color-orangeDark'><strong>"
								+ $('#show-shortcut').text()
								+ "</strong></span> ?",
						content : "确认要删除相应用例？请谨慎操作",
						buttons : '[No][Yes]'
					},
					function(ButtonPressed) {
						if (ButtonPressed == "Yes") {
							$
									.ajax({
										url : '/smarttest-frontstage/AutoInterface/case/delete.action',
										dataType : 'json',
										method : 'POST',
										data : {
											idsStr : info
										},
										success : function(data) {
											fetchTable('', '', '', '', 0);
										}
									});
						}
					});
}

function showEndInfo() {
	$('#reach_end').show();
	setTimeout("$('#reach_end').hide();", 3000);
}

function getNowFormatDate(ns) {
	var date = new Date(ns);
	var seperator1 = "-";
	var seperator2 = ":";
	var month = date.getMonth() + 1;
	var strDate = date.getDate();
	if (month >= 1 && month <= 9) {
		month = "0" + month;
	}
	if (strDate >= 0 && strDate <= 9) {
		strDate = "0" + strDate;
	}
	var currentdate = date.getFullYear() + seperator1 + month + seperator1
			+ strDate + " " + date.getHours() + seperator2 + date.getMinutes()
			+ seperator2 + date.getSeconds();
	return currentdate;
}

function search() {
	var level = $("#caseLevelSearch").val();
	if (event.keyCode == 13) {
		event.returnValue = false;
		event.cancel = true;
		fetchTable('', '', '', '', level);
	}
}

function alertDialog(title, content) {
	$.SmartMessageBox({
		title : "<i class='fa fa-sign-out txt-color-orangeDark'></i>" + title
				+ "<span class='txt-color-orangeDark'><strong>"
				+ $('#show-shortcut').text() + "</strong></span>",
		content : content,
		buttons : '[确定]'
	}, function(ButtonPressed) {
		if (ButtonPressed == "确定") {
		}
	});
}

function executeCase(each) {
	initDataDialog(each.id);
	$('#casedatamodal').modal();
}

function initDataDialog(caseid, level, searchValue) {
	alert(caseid);
	fetchTableData(caseid, level, searchValue)
}

function fetchTableData(caseid, level, searchValue) {
	$('#loadingData').show();
	isEnd = true;
	// var searchvalue = $("#doInput").val();
	var pageSize = $('#dt_row_data').val();
	var toDataPage = 1;
	var totalDataPage = 1;
	$
			.ajax({
				url : '/smarttest-frontstage/AutoInterface/search/data.action',
				dataType : 'json',
				method : 'POST',
				data : {
					caseid : caseid,
					level : level,
					searchValue : searchValue,
					pageSize : pageSize,
					toPage : toDataPage,
				},
				success : function(data) {
					oper_data_ids = [];
					$("#test_data_body").html("");
					var msg = data.rows;
					var i = 0;
					var html = '';
					$
							.each(
									msg,
									function(k, v) {
										var item = msg[i];
										if (i % 2 === 0) {
											html += '<tr bgcolor="#DFEBF2">';
										} else {
											html += '<tr>';
										}
										html += '<td>'
												+ "<input type=\"checkbox\" class=\"casestable\" onclick=\"javascript:singleCheckData(this);\" value=\""
												+ item.id + "\"/>" + '</td>';
										html += '<td>' + item.name + '</td>';
										html += '<td>' + item.clazz + '</td>';
										html += '<td>' + item.scens + '</td>';
										html += '<td>' + item.owner + '</td>';
										html += '<td>' + item.remark + '</td>';
										html += '<td>' + item.level + '</td>';
										html += '<td>'
												+ getNowFormatDate(item.created)
												+ '</td>';
										html += '<td>'
												+ getNowFormatDate(item.updated)
												+ '</td>';
										html += '</tr>';
										i++;
									});
					$("#test_data_body").append(html);
					$('#loadingData').hide();
				}
			});
}

function singleCheckData(checkbox) {
	if (checkbox.checked) {
		if (-1 == $.inArray(checkbox.value, del_case_ids)) {
			oper_data_ids.push(checkbox.value);
		}
	} else {
		var i = $.inArray(checkbox.value, del_case_ids);
		if (i > -1) {
			oper_data_ids.splice(i, 1);
		}
	}
}

function uploadFile() {
	var form = $("form[name=caseupload]");
	var options = {
		url : '/smarttest-frontstage/AutoInterface/case/upload.action',
		type : 'post',
		dataType : 'json',
		success : function(data) {
			if(data.error==0&&data.url){
				$('#caseurl').val(data.url);
				$('#uploadLabel').html("");
				$('#uploadLabel').html("上传成功");
				pageAddCaseControl(true,true,false,false,false,false,false,false,false,false);
			}else{
				$('#uploadLabel').html("");
				$('#uploadLabel').html("上传失败");
			}
		}
	};
	form.ajaxSubmit(options);
}



function pageAddCaseControl(val1, val2, val3, val4, val5, val6, val7, val8, val9, val10) {
	if (val1 == true) {
		$("#caseupload").attr("disabled", "disabled");
	} else {
		$("#caseupload").removeAttr("disabled");
	}
	if (val2 == true) {
		$("#upload").attr("disabled", "disabled");
	} else {
		$("#upload").removeAttr("disabled");
	}
	if (val3 == true) {
		$("#name").attr("disabled", "disabled");
	} else {
		$("#name").removeAttr("disabled");
	}
	if (val4 == true) {
		$("#clazz").attr("disabled", "disabled");
	} else {
		$("#clazz").removeAttr("disabled");
	}
	if (val5 == true) {
		$("#scenes").attr("disabled", "disabled");
	} else {
		$("#scenes").removeAttr("disabled");
	}
	if (val6 == true) {
		$("#owner").attr("disabled", "disabled");
	} else {
		$("#owner").removeAttr("disabled");
	}
	if (val7 == true) {
		$("#caseLevel").attr("disabled", "disabled");
	} else {
		$("#caseLevel").removeAttr("disabled");
	}
	if (val8 == true) {
		$("#caseLevel").attr("disabled", "disabled");
	} else {
		$("#caseLevel").removeAttr("disabled");
	}
	if (val9 == true) {
		$("#remark").attr("disabled", "disabled");
	} else {
		$("#remark").removeAttr("disabled");
	}
	if (val10 == true) {
		$("#addCase").attr("disabled", "disabled");
	} else {
		$("#addCase").removeAttr("disabled");
	}
}

function downLoadCase(obj){
	if(obj.is(':checked')){
	$('#caseupload').show();
	$("#downLoadSingleCase").submit();
	pageAddCaseControl(false,false,true,true,true,true,true,true,true,true);
	}
}
